package com.paomi.client.activity.person;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.address.ReceivingAddressActivity;
import com.paomi.client.activity.person.View.PersonMsgView;
import com.paomi.client.activity.person.model.UploadBean;
import com.paomi.client.activity.person.presenter.PersonMsgPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.AddressListInfo;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PersonMsgActivity extends BaseMvpActivity<PersonMsgView, PersonMsgPre> implements PersonMsgView {
    @BindView(R.id.tv_arrow_head)
    TextView tv_arrow_head;
    @BindView(R.id.tv_arrow_nickname)
    TextView tv_arrow_nickname;
    @BindView(R.id.tv_arrow_birthday)
    TextView tv_arrow_birthday;
    @BindView(R.id.tv_birthday)
    TextView tv_birthday;
    @BindView(R.id.tv_arrow_address)
    TextView tv_arrow_address;
    @BindView(R.id.tv_arrow_phone)
    TextView tv_arrow_phone;
    @BindView(R.id.tv_arrow_password)
    TextView tv_arrow_password;
    @BindView(R.id.tv_man)
    TextView tv_man;
    @BindView(R.id.tv_woman)
    TextView tv_woman;
    @BindView(R.id.button_goback)
    TextView button_goback;
    @BindView(R.id.tv_phone)
    TextView tv_phone;

    private Dialog headDialog;

    //显示的view
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.iv_head)
    RoundedImageView iv_head;
    @BindView(R.id.tv_address)
    TextView tv_address;

    private final int SET_NAME = 4;

    public String sex;
    public String birthday;
    private String nickName;

    @NonNull
    @Override
    public PersonMsgPre createPresenter() {
        return new PersonMsgPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.mine_msg);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_msg;
    }

    @Override
    protected void initEvent() {
        initTextType();

    }

    @OnClick(R.id.ll_head)
    void chooseHead() {
        headDialog = new Dialog(PersonMsgActivity.this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(PersonMsgActivity.this).inflate(R.layout.dialog_head_bottom, null);
        headDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        headDialog.setCanceledOnTouchOutside(true);
        headDialog.getWindow().setGravity(Gravity.BOTTOM);
        TextView album = headDialog.findViewById(R.id.tv_album);
        TextView camera = headDialog.findViewById(R.id.tv_camera);
        TextView cancel = headDialog.findViewById(R.id.tv_cancel);
        album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                headDialog.dismiss();
                getPresenter().getImageFromCamera(PersonMsgActivity.this);
            }
        });
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                headDialog.dismiss();
                getPresenter().getCameraImage(PersonMsgActivity.this);
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (headDialog != null) {
                    headDialog.cancel();
                }
            }
        });
        headDialog.show();
    }

    @OnClick(R.id.ll_address)
    void address() {
        Intent i = new Intent(this, ReceivingAddressActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.ll_man)
    void chooseMan() {
        Map<String, String> map = new HashMap<>();
        sex = 1 + "";
        map.put("sex", sex);
        tv_man.setText(getResources().getString(R.string.check_sex));
        tv_man.setTextColor(getResources().getColor(R.color.main_color));
        tv_woman.setText(getResources().getString(R.string.uncheck_sex));
        tv_woman.setTextColor(getResources().getColor(R.color.color_c9c9c9));
        getPresenter().setUserInfo(map);
    }

    @OnClick(R.id.ll_woman)
    void chooseWoman() {
        Map<String, String> map = new HashMap<>();
        sex = 2 + "";
        map.put("sex", sex);
        tv_woman.setText(getResources().getString(R.string.check_sex));
        tv_woman.setTextColor(getResources().getColor(R.color.main_color));
        tv_man.setText(getResources().getString(R.string.uncheck_sex));
        tv_man.setTextColor(getResources().getColor(R.color.color_c9c9c9));
        getPresenter().setUserInfo(map);
    }

    @OnClick(R.id.ll_nickName)
    void setName() {
        Intent i = new Intent(this, SetNameActivity.class);
        if (!tv_nickname.getText().toString().trim().isEmpty()) {
            i.putExtra("name", tv_nickname.getText().toString().trim());
        }
        startActivityForResult(i, SET_NAME);
    }

    @OnClick(R.id.ll_birthday)
    void setBirthday() {
        getPresenter().selectDate(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SET_NAME) {
            if (resultCode == Activity.RESULT_OK) {
                nickName = data.getStringExtra("nickName");
                Map<String, String> map = new HashMap<>();
                map.put("name", nickName);
                getPresenter().setUserInfo(map);
            }
        } else if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            // 图片选择结果回调
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            if (selectList != null && selectList.size() > 0) {
                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                if(selectList.get(0).isCut()){
                    String msg = selectList.get(0).getCutPath();
                    getPresenter().uploadMineImg(msg);
                }
            }
        }
    }

    void initTextType() {
        tv_arrow_head.setTypeface(iconfont);
        tv_arrow_nickname.setTypeface(iconfont);
        tv_arrow_birthday.setTypeface(iconfont);
        tv_arrow_address.setTypeface(iconfont);
        tv_arrow_phone.setTypeface(iconfont);
        tv_arrow_password.setTypeface(iconfont);
        tv_man.setTypeface(iconfont);
        tv_woman.setTypeface(iconfont);
        button_goback.setTypeface(iconfont);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        headDialog = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", "1");
        map.put("pageSize", "" + Constants.PAGE_SIZE);
        getPresenter().getAddressNum(map);

        //加载用户信息
        if (UserUtils.getInstance().getUserInfo1() != null) {
            UserInfo info = UserUtils.getInstance().getUserInfo1();
            if (info != null) {
                if (info.getHeadimgurl() != null) {
                    GlideApp.with(this).asBitmap().load(info.getHeadimgurl()).placeholder(R.mipmap.img_default_head).into(iv_head);
                }
                if (info.getUserName() != null) {
                    tv_nickname.setText(info.getUserName());
                }
                if (info.getSex() != null) {
                    if (info.getSex().equals("1")) {
                        tv_man.setText(getResources().getString(R.string.check_sex));
                        tv_man.setTextColor(getResources().getColor(R.color.main_color));
                        tv_woman.setText(getResources().getString(R.string.uncheck_sex));
                        tv_woman.setTextColor(getResources().getColor(R.color.color_c9c9c9));
                    } else {
                        tv_woman.setText(getResources().getString(R.string.check_sex));
                        tv_woman.setTextColor(getResources().getColor(R.color.main_color));
                        tv_man.setText(getResources().getString(R.string.uncheck_sex));
                        tv_man.setTextColor(getResources().getColor(R.color.color_c9c9c9));
                    }
                }

                if (info.getBirthday() != null && !info.getBirthday().equals("")) {
                    tv_birthday.setText(info.getBirthday());
                }else{
                    tv_birthday.setText(getResources().getString(R.string.none_write));
                }
                if (info.getMobile() != null && !info.getMobile().equals("")) {
                    tv_phone.setText(info.getMobile().substring(0, 3) + "****" + info.getMobile().substring(7, 11));
                }
            }
        }
    }

    @Override
    public void showChangeUserInfo(BaseJSON result) {
        if (result.returnCode.equals("0000")) {
            if (sex != null && !sex.equals("")) {
                UserUtils.getInstance().getUserInfo1().setSex(sex);
            }
            if (birthday != null && !birthday.equals("")) {
                UserUtils.getInstance().getUserInfo1().setBirthday(birthday);
            }
            if (nickName != null && !nickName.equals("")) {
                UserUtils.getInstance().getUserInfo1().setUserName(nickName);
                tv_nickname.setText(nickName);
            }
            ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.change_success));
        } else {
            Util.toast(this, result.returnMessage);
        }
    }

    @Override
    public void showBirthday(String birthday) {
        tv_birthday.setText(birthday);
        this.birthday = birthday;
        Map<String, String> map = new HashMap<>();
        map.put("birthday", birthday);
        getPresenter().setUserInfo(map);
    }

    @Override
    public void showAddressNum(AddressListInfo s) {
        if (s.returnCode.equals("0000")) {
            if (s.getReturnData() != null) {
                tv_address.setText(s.getReturnData().getCount() + getResources().getString(R.string.address_num));
            }
        } else {
            Util.toast(this, s.returnMessage);
        }
    }

    @Override
    public void showHeadImgUrl(UploadBean.UploadImg bean) {
        Map<String, String> map = new HashMap<>();
        map.put("img", bean.getFileAllUrl());
        getPresenter().setUserInfo(map);
        if (bean.getFileAllUrl() != null && !bean.getFileAllUrl().equals("")) {
            //取值
            UserInfo userInfo=UserUtils.getInstance().getUserInfo1();
            //赋值
            userInfo.setHeadimgurl(bean.getFileAllUrl());
            //存储
            UserUtils.getInstance().saveUser1(userInfo);
            Glide.with(iv_head.getContext())
                    .load(bean.getFileAllUrl())
                    .into(iv_head);
        }
    }
}